package tbdp.tool;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import tbdp.tool.common.Utils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by xbkaishui on 16/10/15.
 * 修复文件格式
 */
public class Repair {

    //上证交易所头信息
    private static String[] shCols =
        new String[] {"", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12",
            "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20", "S21", "S22", "S23", "S24",
            "S25", "S26", "S27", "S28", "S29", "S30", "S31", "S32", "S33"};

    //深证交易所头信息
    private static String[] szCols =
        new String[] {"", "HQZQDM", "HQZQJC", "HQZRSP", "HQJRKP", "HQZJCJ", "HQCJSL", "HQCJJE",
            "HQCJBS", "HQZGCJ", "HQZDCJ", "HQSYL1", "HQSYL2", "HQJSD1", "HQJSD2", "HQHYCC",
            "HQSJW5", "HQSSL5", "HQSJW4", "HQSSL4", "HQSJW3", "HQSSL3", "HQSJW2", "HQSSL2",
            "HQSJW1", "HQSSL1", "HQBJW1", "HQBSL1", "HQBJW2", "HQBSL2", "HQBJW3", "HQBSL3",
            "HQBJW4", "HQBSL4", "HQBJW5", "HQBSL5"};
    private static Logger logger = Logger.getLogger(Repair.class);

    /**
     * 修复文件 根据不同的市场类型对文件做不同的处理
     * <p/>
     * marketType sz ,sh
     * dbf 文件格式 第一行是文件头 包含数据交易时间 和行情状态信息
     *
     * @param dataMap
     * @param marketType
     * @return
     */
    public static List<List<String>> repair(List<Map<String, String>> dataMap, String marketType) {
        List<List<String>> rs = new LinkedList<>();
        Map<String, String> header = dataMap.get(0);
//        System.out.println("header "+ header);

        //SH / SZ 分开处理
        if (StringUtils.equalsIgnoreCase(marketType, "sh")) {
            repairSH(dataMap, marketType, rs, header);
        } else {
            //处理深证数据
            repairSZ(dataMap, marketType, rs, header);
        }
        return rs;
    }

    private static void repairSH(List<Map<String, String>> dataMap, String marketType,
        List<List<String>> rs, Map<String, String> header) {
        String date = "";
        String time = "";
        String millis = ".000";
        String curStatus = "";
        String MDTime = "";
        String timestamp = "";
        //处理头字段
        date = header.remove("S6");
        time = Utils.reformatTime(header.remove("S2"));
        String status = header.remove("S11").trim();
        if (StringUtils.equals(status, "0")) {
            curStatus = "T11";
        } else if (StringUtils.equals(status, "1111111111")) {
            curStatus = "E111";
        }
        MDTime = date + "-" + time + millis;
        timestamp = time + millis;


        for (int i = 1; i < dataMap.size(); i++) {
            Map<String, String> row = dataMap.get(i);
            String scode = row.get(shCols[1]);
            String MDStreamID = generateStreamId(scode, marketType);
            // MD001的数据输出 15位 其它都是35位 特殊处理下
            if (StringUtils.equalsIgnoreCase(MDStreamID, "MD001")) {
                rs.add(Arrays.asList(MDStreamID, scode, row.get(shCols[2]),
                    Utils.validNumber(row.get(shCols[11])), Utils.validNumber(row.get(shCols[5])),
                    Utils.validNumber(row.get(shCols[3])), Utils.validNumber(row.get(shCols[4])),
                    Utils.validNumber(row.get(shCols[6])), Utils.validNumber(row.get(shCols[7])),
                    Utils.validNumber(row.get(shCols[8])), Utils.validNumber(row.get(shCols[8])),
                    curStatus, timestamp, MDTime, curStatus));
            } else {
                rs.add(Arrays.asList(MDStreamID, scode, row.get(shCols[2]),
                    Utils.validNumber(row.get(shCols[11])), Utils.validNumber(row.get(shCols[5])),
                    Utils.validNumber(row.get(shCols[3])), Utils.validNumber(row.get(shCols[4])),
                    Utils.validNumber(row.get(shCols[6])), Utils.validNumber(row.get(shCols[7])),
                    Utils.validNumber(row.get(shCols[8])), Utils.validNumber(row.get(shCols[8])),
                    //买卖
                    Utils.validNumber(row.get(shCols[9])), Utils.validNumber(row.get(shCols[15])),
                    Utils.validNumber(row.get(shCols[10])), Utils.validNumber(row.get(shCols[21])),

                    Utils.validNumber(row.get(shCols[16])), Utils.validNumber(row.get(shCols[17])),
                    Utils.validNumber(row.get(shCols[22])), Utils.validNumber(row.get(shCols[23])),

                    Utils.validNumber(row.get(shCols[18])), Utils.validNumber(row.get(shCols[19])),
                    Utils.validNumber(row.get(shCols[24])), Utils.validNumber(row.get(shCols[25])),

                    Utils.validNumber(row.get(shCols[26])), Utils.validNumber(row.get(shCols[27])),
                    Utils.validNumber(row.get(shCols[30])), Utils.validNumber(row.get(shCols[31])),

                    Utils.validNumber(row.get(shCols[28])), Utils.validNumber(row.get(shCols[29])),
                    Utils.validNumber(row.get(shCols[32])), Utils.validNumber(row.get(shCols[33])),
                    curStatus, timestamp, MDTime, curStatus));
            }
        }
    }

    private static void repairSZ(List<Map<String, String>> dataMap, String marketType,
        List<List<String>> rs, Map<String, String> header) {
        String date = "";
        String time = "";
        String millis = ".000";
        String curStatus = "";
        String MDTime = "";
        String timestamp = "";
        //处理头字段
        date = header.remove(szCols[2]);
        time = Utils.reformatTime(header.remove(szCols[8]));
        String status = header.remove(szCols[6]).trim();
        int statusInt = Integer.parseInt(status) % 10;
        if (StringUtils.equals(status, "0") || statusInt == 0) {
            curStatus = "T11";
        } else if (StringUtils.equals(status, "1") || statusInt ==1) {
            curStatus = "E";
        }
        MDTime = date + "-" + time + millis;
        timestamp = time + millis;


        for (int i = 1; i < dataMap.size(); i++) {
            Map<String, String> row = dataMap.get(i);
            String scode = row.get(szCols[1]);
            String MDStreamID = generateStreamId(scode, marketType);
            rs.add(Arrays.asList(MDStreamID, scode, row.get(szCols[2]),
                Utils.validNumber(row.get(szCols[6])), Utils.validNumber(row.get(szCols[7])),
                Utils.validNumber(row.get(szCols[3])), Utils.validNumber(row.get(szCols[4])),
                Utils.validNumber(row.get(szCols[9])), Utils.validNumber(row.get(szCols[10])),
                Utils.validNumber(row.get(szCols[5])), Utils.validNumber(row.get(szCols[5])),
                //买卖
                Utils.validNumber(row.get(szCols[26])), Utils.validNumber(row.get(szCols[27])),
                Utils.validNumber(row.get(szCols[24])), Utils.validNumber(row.get(szCols[25])),

                Utils.validNumber(row.get(szCols[28])), Utils.validNumber(row.get(szCols[29])),
                Utils.validNumber(row.get(szCols[22])), Utils.validNumber(row.get(szCols[23])),

                Utils.validNumber(row.get(szCols[30])), Utils.validNumber(row.get(szCols[31])),
                Utils.validNumber(row.get(szCols[20])), Utils.validNumber(row.get(szCols[21])),

                Utils.validNumber(row.get(szCols[32])), Utils.validNumber(row.get(szCols[33])),
                Utils.validNumber(row.get(szCols[18])), Utils.validNumber(row.get(szCols[19])),

                Utils.validNumber(row.get(szCols[34])), Utils.validNumber(row.get(szCols[35])),
                Utils.validNumber(row.get(szCols[16])), Utils.validNumber(row.get(szCols[17])),
                curStatus, timestamp, MDTime, curStatus));
        }
    }

    /**
     * 根据股票id 和市场类型来合成 MDStreamID
     * sz 不需要处理
     *
     * @param scode
     * @param marketType
     * @return
     */
    private static String generateStreamId(String scode, String marketType) {

        if (StringUtils.equalsIgnoreCase(marketType, "sh")) {
            if (scode.startsWith("00")) {
                return "MD001";
            }
        }
        return "MD003";
    }
}
